//没这个访问不了这里面的函数
module.exports = {
    openShare: openShare,
    sx_alert: sx_alert,
    sx_toast: sx_toast,
    sx_wxTitle: sx_wxTitle,
    sx_getCache: sx_getCache,
    sx_setCache: sx_setCache,
    sx_ajax: sx_ajax,
    chatNick: chatNick,
    changeTab: changeTab,
    goBackPage: goBackPage,
    setDD: setDD,
    trim: trim,
    IdCard:IdCard,
    porpDiv:porpDiv,
    MYIMG:MYIMG,
    ltrim: ltrim,
    rtrim: rtrim,
    mytime2: mytime2,
    sx_confirm: sx_confirm,
    zfill: zfill,
    getListCnt:getListCnt,
    getRestTime22: getRestTime22,
    passTime1: passTime1,
    tleMi:tleMi,
    sximg: sximg,
    floatMathFun:floatMathFun,
  }
  function floatMathFun(num1, num2, key){
    // 计算加法
    if(key=="+"){
      var res = ((num1 * 100 + num2 * 100) / 100).toFixed(2);
    }
    // 计算减法
    if(key=="-"){
      var res = ((num1 * 100 - num2 * 100) / 100).toFixed(2);
    }
    // 计算乘法
    if(key=="*"){
      var res = ((num1 * 100 * (num2 * 100)) / (100 * 100)).toFixed(2);
    }
    // 计算除法
    if(key=="/"){
      var res = ((num1 * 100) / (num2 * 100)).toFixed(2);
    }
    return res;
  }
  function tleMi(telStr){
    var telSS1 = String(telStr);
    var telSS2 = String(telStr);
    if(telSS1!=""){
      return telSS1.substring(0,3)+"****"+telSS2.substring(7);
    }
    else{
      return telSS1;
    }
  }
  function openShare(){
    wx.showShareMenu({
      withShareTicket: true
    })
  }
  function sx_alert(msg,title,fun){
    var tt = title != undefined ? title : '提示';
    wx.showModal({
      title: tt,
      content: String(msg),
      showCancel: false,
      complete:function(){
        if (fun != undefined) {
          fun();
        }
      }
    })
  }
  function sx_confirm(str,okFun,noFun){
    wx.showModal({
      title: '提示',
      content: str,
      success: function (sm) {
        if (sm.confirm) {
          if (okFun!=undefined){
            okFun();
          }
        } else if (sm.cancel) {
          if (noFun != undefined) {
            noFun();
          }
        }
      }
    })
  }
  /**无按钮，提示后自动消失 */
  function sx_toast(msg){
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  }
  function sx_wxTitle(titStr){
    //sx_toast('2222');
    wx.setNavigationBarTitle({
      title: titStr
    })
  }
  function sx_getCache(key,deval){
    var getval = wx.getStorageSync(String(key));
    var back = getval == undefined ? deval : getval;
    return back;
  }
  function sx_setCache(key,data) {
    wx.setStorageSync(String(key), data)
  }
  //返回页面
  function goBackPage(bNum,okFun){
    if (okFun!=undefined){
      wx.navigateBack({
        delta: bNum,
        success:okFun
      });
    }
    else{
      wx.navigateBack({
        delta: bNum
      });
    }
  }
  //切换tab
  function changeTab(urlVal,okFun){
    wx.switchTab({ url: urlVal, success:okFun});
  }
  function sx_ajax(url,dataP,fun, noFun){
    var trueUlr = http_host + url;
    if (url.indexOf('http')>-1){
      trueUlr = url;
    }
    wx.request({
        url: trueUlr,//接口
        method: 'post',
        header: {
          //换成json就出错，不知道什么鬼
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: dataP,
        success: function (res) {
          //console.log('提交成功', res);
          var ret = res.data;
          if (!ret.flag) {
            if (noFun != undefined) {
              noFun(ret);
            }
            else{
              sx_alert(ret.msg);
            }
          }
          else{
            //sx_alert(ret.data.liveUrl);
            if (fun != undefined) {
              fun(ret.data);
            }
          }
        },
        fail:function(res){
          if (noFun != undefined) {
            noFun(ret);
          }
        }
    });
  }
  //聊天消息昵称设置
  function chatNick(str){
    if (str.indexOf('@@@')>0){
      var arr = str.split('@@@');
      return arr[0];
    }
    else{
      return str;
    }
  }
  //构造头像
  function sximg(url){
    var trueUlr = wx.getStorageSync('YU') + url;
    if(url=="" || url==undefined || url==null){
      trueUlr = ""
    }
    else{
      if (url.indexOf('http') > -1) {
        trueUlr = url;
      }
    }
    return trueUlr;
  }
  function MYIMG(YU,url){
    var trueUlr = YU + url;
    if(url=="" || url==undefined || url==null){
      trueUlr = ""
    }
    else{
      if (url.indexOf('http') > -1) {
        trueUlr = url;
      }
    }
    return trueUlr;
  }
  function zfill(num, size) {
    var s = "000000000" + String(num);
    return s.substr(s.length - size);
  }
  function mytime2(timeStr) {
    var str = "";
    var time = parseInt(timeStr);
    if (time > 0) {
      var nowDate = new Date();
      nowDate.setTime(time * 1000);
      var nowYY = nowDate.getFullYear();
      var nowMM = this.zfill(nowDate.getMonth() + 1, 2);
      var nowDD = this.zfill(nowDate.getDate(), 2);
      var nowShi = this.zfill((nowDate).getHours(), 2);
      var nowfen = this.zfill((nowDate).getMinutes(), 2);
      var nowmiao = this.zfill((nowDate).getSeconds(), 2);
  
      //str = nowYY+'-'+nowMM+'-'+nowDD+' '+nowShi+':'+nowfen+':'+nowmiao;
      str = nowMM + '月' + nowDD + '日 ' + nowShi + '-' + nowfen;
    }
    return str;
  }
  function getRestTime22(timeStr, type) {
    var str = "";
    var time = parseInt(timeStr);
    switch (type) {
      case 1: {
        str = parseInt((time / (3600 * 24)));
        break;
      }
      case 2: {
        str = parseInt((time - parseInt(time / (3600 * 24)) * 3600 * 24) / 3600);
        break;
      }
      case 3: {
        str = parseInt((time - (parseInt((time - parseInt(time / (3600 * 24)) * 3600 * 24) / 3600) * 3600) - (parseInt(time / (3600 * 24)) * 3600 * 24)) / 60);
        break;
      }
    }
    str = str <= 0 ? 0 : str;
    return str;
  }
  function passTime1(yatime) {
    var date = new Date();
    var nowDate = parseInt(date.getTime() / 1000);
    var miao = nowDate - parseInt(yatime);
    var back = '';
    if (miao < 60) {
      back = miao + "秒之前";
    }
    else if (miao < 3600) {
      var fen = this.getRestTime22(miao, 3);
      back = fen + "分钟之前";
    }
    else if (miao < 3600 * 10) {
      var xiaoshi = this.getRestTime22(miao, 2);
      var fen = this.getRestTime22(miao, 3);
      //back = xiaoshi + "小时" + fen + "分钟之前";
      back = xiaoshi + "小时之前";
    }
    else {
      back = this.mytime2(yatime);
    }
    return back;
  }
  function setDD(obj, key, val) {
    obj.setData({ key: val });
  }
  //删除左右两端的空格
  function trim(str) {
    return str!="" && str!=null && str!=undefined ? str.replace(/(^\s*)|(\s*$)/g, "") : ""
  }
  //删除左边的空格
  function ltrim(str) {
    return str!="" && str!=null && str!=undefined ? str.replace(/(^\s*)/g, "") : ""
  }
  //删除右边的空格
  function rtrim(str) {
    return str!="" && str!=null && str!=undefined ? str.replace(/(\s*$)/g, "") : ""
  }
// 弹出输入款验证
  function porpDiv(_title,_pwdTip, _pwd,okFun){
    wx.showModal({
        title:_title,
        editable:true,//显示输入框
        placeholderText:_pwdTip,//显示输入框提示信息
        success: res => {
            if (res.confirm) { //点击了确认
                console.log(res.content)//用户输入的值
                if(res.content==_pwd){
                    if(okFun!=undefined){
                        okFun()
                    }
                }
                else{
                    sx_alert(_pwdTip);
                }
            } else {
                console.log('用户点击了取消')
            }
        }
    })
  }
// 获取对象cnt
function getListCnt(arr,key,val){
  var back = 0
  for(var i=0,cnt=arr.length; i<cnt; i++){
    if(arr[i][key]==val){
      back = i
      break
    }
  }
  return back
}
// 根据身份证号获取性别
function IdCard(UUserCard, num){
    var len = (UUserCard + "").length;
    if (len == 0) {
       // return '暂无';
       return -1
    }
    if (len != 18)//身份证号码只能为15位或18位其它不合法
    {
       // return '暂无';
       return -1
    }
    if(!(/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(UUserCard))){
       // return '暂无';
       return -1
    }

    if (num == 1) {
        //获取出生日期
        birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
        return birth;
    }
    
    if (num == 2) {
        //获取性别
        if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
            // return "男";
            return 1;
        } else {
            // return "女";
            return 2;
        }
    }

    if (num == 3) {
        //获取年龄
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
        if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
            age++;
        }
        return age;
    }
}